package com.qianfeng.openapi.web.master.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.openapi.web.master.mapper.RoleMapper;
import com.qianfeng.openapi.web.master.pojo.Role;
import com.qianfeng.openapi.web.master.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author LJF
 * @Date 2020/7/4 8:59
 * @Version 1.0
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageInfo<Role> getAllRoles(Integer page,Integer limit) {
        PageHelper.startPage(page,limit);
        List<Role> allRoles = roleMapper.getAllRoles();
        return new PageInfo<>(allRoles);
    }

    @Override
    public int insertRole(Role role) {
        return roleMapper.insertRole(role);
    }

    @Override
    public int updateRole(Role role) {
        return roleMapper.updateRole(role);
    }

    @Override
    public PageInfo<Role> getRoleByCondition(Integer page, Integer limit,String name,Integer status) {
        PageHelper.startPage(page,limit);
        List<Role> roles = roleMapper.getRoleByCondition(name, status);
        return new PageInfo<>(roles);
    }

    @Override
    public List<Integer> getMenuIdsByRoleId(Integer roleId) {
        return roleMapper.getMenuIdsByRoleId(roleId);
    }

    @Override
    public void updateRoleMenu(Integer roleId, Integer[] menuIds) {
        //清除中间表
        roleMapper.deleteRoleMenuByRoleId(roleId);  //删除某个角色的所有菜单
        for (Integer menuId : menuIds) {
            roleMapper.insertRoleMenu(roleId,menuId);  //给某个角色加菜单
        }
    }

}
